<?php
  require_once('./includes/header.php');

  if(!isset($_SESSION['id']) || empty($_SESSION['id'])){
    header("location:login.php");
    exit;
  }
  $permittedTbl = array('users','countries');

  $tbl = isset($_GET['tbl']) && !empty($_GET['tbl']) ? trim($_GET['tbl']) : '';

  if($tbl == '' || !in_array($tbl, $permittedTbl)){
    jsRedirect('show_all_users.php');
  }

if(isset($_FILES['filename'])){

  $returnData = upload('filename',$uploadDir = './uploads',
  array('csv'),$allowedSize = 2.0 );

  $fileName = $returnData['fileName'];

$fp = fopen("uploads/$fileName",'r');
var_dump($fp);
$count = 0;

$cols = fetchColNames($tbl);

while($allRecords = fgetcsv($fp, 4096)){
  if($count++ != 0){
    $allRecords[0] = null;

    $allRecords = array_combine($cols, $allRecords);
    //pr1($allRecords);
    $allRecords = arrayCleanup($allRecords);
    insert($tbl,$allRecords);
  }
}
$count--;
$msg = "<strong>$count</strong> Records Imported successfully";
fclose($fp);

unlink("uploads/$fileName");
}
?>

<div class="row">
<?php
if($msg != '') {
?>
    <div class="alert alert-success fade in">
      <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
      <p><?=$msg?></p>
    </div>
<?php
}
?>
</div>


    <form enctype="multipart/form-data" class="navbar-form navbar-left" method="post" action="" >
      <div class="form-group">
        <input type="file" class="form-control" name="filename" >
      </div>
      <button type="submit" class="btn btn-primary">Import&nbsp;<span class="glyphicon glyphicon-import"></span></button>
    </form>

